package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.LuckDrawListAdapter;
import com.yanq.liancar.bean.FindOdbBindBean;
import com.yanq.liancar.bean.LuckDrawListBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LotteryRecordActivity extends BaseToolbarActivity {
    @BindView(R.id.action_bar)
    AppBarLayout actionBar;
    @BindView(R.id.order_recyclerview)
    RecyclerView orderRecyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    private LuckDrawListAdapter adapter;
    private List<LuckDrawListBean.DataBean.ListBean> luckDrawListBeanList = new ArrayList<>();
    private int page = 1;
    private int pages;
    private boolean isClear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lotteryrecord);
        Utils.setStatusBar(this, true, false);
        ButterKnife.bind(this);
        mTvTitle.setText("中奖记录");

        initRecyclerview();
        initListener();
        initOkGo();
    }


    private void initRecyclerview() {

        orderRecyclerview.setNestedScrollingEnabled(false);
        orderRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new LuckDrawListAdapter(R.layout.item_luckdraw, luckDrawListBeanList);
        orderRecyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    private void initListener() {
        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;

                initOkGo();
                refreshlayout.finishRefresh();
            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
                if (pages >= page) {
                    initOkGo();
                }else {
                    RxToast.normal("暂无更多数据");
                }
                refreshLayout.finishLoadMore();
            }

        });
    }

        private void initOkGo() {
            OkGo.<String>post(Config.s + "llc/api/WinningController/selectMyWinningRecord")
                    .tag(this)
                    .params("token", UserBean.getcToken(this))
                    .params("pageNum", page)
                    .params("pageSize", 10)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            LogUtils.e("qeqweqe", response.body());
                            Gson gson = new Gson();
                            LuckDrawListBean info = gson.fromJson(response.body(), LuckDrawListBean.class);
                            if (info.isIsSuccess()) {
                                if (info.getCode()==200){
                                    pages=info.getData().getPages();
                                    if (isClear){
                                        luckDrawListBeanList.clear();
                                    }
                                    luckDrawListBeanList.addAll(info.getData().getList());
                                    adapter.notifyDataSetChanged();
                                }else {
                                    RxToast.normal(info.getOutputMessage());
                                }
                            } else {
                                RxToast.error(info.getOutputMessage());
                            }
                        }
                    });
        }




    //    private void initViewPager() {
    //        // 创建一个集合,装填Fragment
    //        List<Fragment> fragments = new ArrayList<>();
    //        // 装填
    //        fragments.add(LuckDrawFragment.newInstance(0));
    //        fragments.add(LuckDrawFragment.newInstance(1));
    //
    //
    //        titles = new ArrayList<>();
    //        titles.add("已中奖");
    //        titles.add("未中奖");
    //
    //        // 创建ViewPager适配器
    //        OrderPagerAdapter myPagerAdapter = new OrderPagerAdapter(getSupportFragmentManager(), fragments, titles);
    //
    //        // 给ViewPager设置适配器
    //        viewPager.setAdapter(myPagerAdapter);
    //        // TabLayout 指示器 (记得自己手动创建4个Fragment,注意是 app包下的Fragment 还是 V4包下的 Fragment)
    //        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(0)), true);
    //        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(1)));
    //
    //        // 使用 TabLayout 和 ViewPager 相关联
    //        mTabLayout.setupWithViewPager(viewPager);
    //        mTabLayout.post(new Runnable() {
    //            @Override
    //            public void run() {
    //                setIndicator(mTabLayout, 70, 70);
    //            }
    //        });
    //    }
    //
    //
    //    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
    //        Class<?> tabLayout = tabs.getClass();
    //        Field tabStrip = null;
    //        try {
    //            tabStrip = tabLayout.getDeclaredField("mTabStrip");
    //        } catch (NoSuchFieldException e) {
    //            e.printStackTrace();
    //        }
    //
    //        tabStrip.setAccessible(true);
    //        LinearLayout llTab = null;
    //        try {
    //            llTab = (LinearLayout) tabStrip.get(tabs);
    //        } catch (IllegalAccessException e) {
    //            e.printStackTrace();
    //        }
    //
    //        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
    //        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
    //
    //        for (int i = 0; i < llTab.getChildCount(); i++) {
    //            View child = llTab.getChildAt(i);
    //            child.setPadding(0, 0, 0, 0);
    //            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
    //            params.leftMargin = left;
    //            params.rightMargin = right;
    //            child.setLayoutParams(params);
    //            child.invalidate();
    //        }
    //    }
}
